﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;

namespace SPPivotViewer.SPPivotViewerWP
{
    public class EditorCollectionPivotViewer : EditorPart
    {

        
        DropDownList ddlList;
        DropDownList ddlType;
        ListBox lstAllFieldFacet;
        ListBox lstSelectFieldFacet;
        ListBox lstAllFieldInfo;
        ListBox lstSelectFieldInfo;
        Button btnAddFieldFacet;
        Button btnRemoveFieldFacet;
        Button btnAddFieldInfo;
        Button btnRemoveFieldInfo;
        DropDownList ddlFieldUrl;
        DropDownList ddlItemOpen;
        DropDownList ddlBaseList;
        Label lblBaseList;
        Table tblInfo;
        Table tblFacet;
        
        
        Label lcOpenItem;
        Label lcFieldUrl;
        Label lcUrlSubst;
        Label lcFieldImage;
        Label lcFieldInfo;
        Label lcChoiceFilter;
        Label lcChoiceList;

        DropDownList ddlImageField;
        TextBox txtImageSubstitue;
        protected override void CreateChildControls()
        {

            this.Title = "Pivot Viewer Collection Configuration";
            ddlType = new DropDownList();
            ddlType.Items.Add(new ListItem("Site Navigation"));
            ddlType.Items.Add(new ListItem("List"));
            ddlType.Items.Add(new ListItem("Site Collection Data"));
            ddlType.SelectedIndexChanged += new EventHandler(ddlType_SelectedIndexChanged);
            ddlType.AutoPostBack = true;
            Label lcCollectionType = new Label();
            lcCollectionType.Text="Choice type of collection<br/>";
            lcCollectionType.CssClass = "UserSectionHead";
            this.Controls.Add(lcCollectionType);
            this.Controls.Add(ddlType);

            this.LoadControlState(this);
            InstanceControl();
            CreatePanForSiteDataCollection();
            CreatePanForListSelection();

        }


        void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            ShowControlCore(false);
            if (ddlType.SelectedValue == "List")
            {
                ShowControlList();
                ddlList_SelectedIndexChanged(this, null);
            }
            if (ddlType.SelectedValue == "Site Collection Data")
            {
                ShowSiteCollectionData();
                
            }
        }

        


        void btnRemoveFieldFacet_Click(object sender, EventArgs e)
        {
            if (lstSelectFieldFacet.SelectedItem == null)
                return;
            for (int miind = 0; miind < lstSelectFieldFacet.Items.Count; miind++)
            {
                if (lstSelectFieldFacet.Items[miind].Selected)
                    lstSelectFieldFacet.Items.Remove(lstSelectFieldFacet.Items[miind]);
            }
            lstSelectFieldFacet.SelectedIndex = -1;
        }

        void btnAddFieldFacet_Click(object sender, EventArgs e)
        {
            if (lstAllFieldFacet.SelectedItem == null)
                return;
            foreach (ListItem item in lstAllFieldFacet.Items)
                if (item.Selected)
                    lstSelectFieldFacet.Items.Add(item);
            lstSelectFieldFacet.SelectedIndex = -1;

        }
        void btnRemoveFieldInfo_Click(object sender, EventArgs e)
        {
            if (lstSelectFieldInfo.SelectedItem == null)
                return;
            for (int miind = 0; miind < lstSelectFieldInfo.Items.Count; miind++)
            {
                if (lstSelectFieldInfo.Items[miind].Selected)
                    lstSelectFieldInfo.Items.Remove(lstSelectFieldInfo.Items[miind]);
            }
            lstSelectFieldInfo.SelectedIndex = -1;
        }

        void btnAddFieldInfo_Click(object sender, EventArgs e)
        {
            if (lstAllFieldInfo.SelectedItem == null)
                return;
            foreach (ListItem item in lstAllFieldInfo.Items)
                if (item.Selected)
                    lstSelectFieldInfo.Items.Add(item);
            lstSelectFieldInfo.SelectedIndex = -1;

        }

        void ddlList_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableControlListSelected(true);


            lstAllFieldFacet.Items.Clear();
            lstSelectFieldFacet.Items.Clear();
            lstAllFieldInfo.Items.Clear();
            lstSelectFieldInfo.Items.Clear();
            ddlImageField.Items.Clear();
            txtImageSubstitue.Text = "";
            ddlFieldUrl.Items.Clear();

            if (ddlList.SelectedValue == "")
                return;
            ddlImageField.Items.Add(new ListItem("Select", ""));
            SPWeb web = SPContext.Current.Web;
            SPList lst = web.Lists[ddlList.SelectedValue];
            ddlImageField.Items.Add(new ListItem("Item Url"));
            ddlFieldUrl.Items.Add(new ListItem("Item Url", ""));
            foreach (SPField field in lst.Fields)
            {
                if (field.InternalName == "File_x0020_Type" ||field.InternalName == "ContentType"|| (field.Type == SPFieldType.Computed && field.InternalName == "FileSizeDisplay"))
                {
                    lstAllFieldFacet.Items.Add(new ListItem(field.Title));
                    lstAllFieldInfo.Items.Add(new ListItem(field.Title));
                }
                if (field.Hidden)
                    continue;
                if (field.Title == "Name" || field.Title == "Title")
                    continue;
                if (field.Type == SPFieldType.URL)
                {
                    ddlImageField.Items.Add(new ListItem(field.Title));
                    ddlFieldUrl.Items.Add(new ListItem(field.Title));
                }
                if (field.Type == SPFieldType.Choice || field.Type == SPFieldType.MultiChoice
                    || field.Type == SPFieldType.DateTime || field.Type == SPFieldType.URL || field.Type == SPFieldType.User || field.Type == SPFieldType.Number
                    || field.Type == SPFieldType.Integer || field.Type == SPFieldType.Currency)
                {
                    lstAllFieldFacet.Items.Add(new ListItem(field.Title));
                    lstAllFieldInfo.Items.Add(new ListItem(field.Title));

                }
            }

        }

        private void EnableControlListSelected(bool enable)
        {
            lstAllFieldFacet.Enabled = enable;
            lstSelectFieldFacet.Enabled = enable;
            lstAllFieldInfo.Enabled = enable;
            lstSelectFieldInfo.Enabled = enable;
            txtImageSubstitue.Enabled = enable;
            btnAddFieldFacet.Enabled = enable;
            btnRemoveFieldFacet.Enabled = enable;
            ddlImageField.Enabled = enable;
            ddlFieldUrl.Enabled = enable;
            ddlItemOpen.Enabled = enable;
        }
        private void ShowControlList()
        {
            ddlList.Visible = true;
            lcChoiceList.Visible = true;
            ShowControlCore(true);
            txtImageSubstitue.Visible = true;
            ddlImageField.Visible = true;
            ddlFieldUrl.Visible = true;
            ddlItemOpen.Visible = true;
            lcOpenItem.Visible = true;
            lcFieldUrl.Visible = true;
            lcUrlSubst.Visible = true;
            lcFieldImage.Visible = true;
            ddlBaseList.Visible = false;
            lblBaseList.Visible = false;

        }
        private void ShowControlCore(bool visible)
        {
            tblInfo.Visible = visible;
            tblFacet.Visible = visible;
            lstAllFieldFacet.Visible = visible;
            lstSelectFieldFacet.Visible = visible;
            lstAllFieldInfo.Visible = visible;
            lstSelectFieldInfo.Visible = visible;
            btnAddFieldFacet.Visible = visible;
            btnRemoveFieldFacet.Visible = visible;
            lcFieldInfo.Visible = visible;
            lcChoiceFilter.Visible = visible;
        }
        
        private void ShowSiteCollectionData()
        {
            ddlList.Visible = false;
            lcChoiceList.Visible = false;
            ShowControlCore(false);
            txtImageSubstitue.Visible = false;
            ddlImageField.Visible = false;
            ddlFieldUrl.Visible = false;
            ddlItemOpen.Visible = false;
            lcOpenItem.Visible = false;
            lcFieldUrl.Visible = false;
            lcUrlSubst.Visible = false;
            lcFieldImage.Visible = false;
            ddlBaseList.Visible = true;
            lblBaseList.Visible = true;

        }
        public override bool ApplyChanges()
        {
            this.EnsureChildControls();
            SPPivotViewerWP pivotWP = (SPPivotViewerWP)this.WebPartToEdit;
            if (!string.IsNullOrEmpty(ddlList.SelectedValue) && ddlType.SelectedValue=="list")
                return false;
            pivotWP.Type = ddlType.SelectedValue;
            pivotWP.ListName = ddlList.SelectedValue;
            List<string> astrSelectedField = new List<string>();
            foreach (ListItem item in lstSelectFieldFacet.Items)
                astrSelectedField.Add(item.Text);
            pivotWP.FieldDisplay = string.Join("|", astrSelectedField.ToArray());

            List<string> astrSelectedInfo = new List<string>();
            foreach (ListItem item in lstSelectFieldInfo.Items)
                astrSelectedInfo.Add(item.Text);
            pivotWP.Infos = string.Join("|", astrSelectedInfo.ToArray());

            if (ddlImageField.SelectedItem == null)
                pivotWP.FieldImage = "";
            else
                pivotWP.FieldImage = ddlImageField.SelectedValue;
            pivotWP.URLImageSubstitue = txtImageSubstitue.Text;
            pivotWP.FieldUrl = ddlFieldUrl.SelectedValue;
            int iItemOpen = 0;
            if (int.TryParse(ddlItemOpen.SelectedValue, out iItemOpen))
                pivotWP.ItemOpen = iItemOpen;
            else
                pivotWP.ItemOpen = 0;
            int iBaseList = 0;
            if (int.TryParse(ddlBaseList.SelectedValue, out iBaseList))
                pivotWP.BaseList = iBaseList;
            else
                pivotWP.BaseList = 0;
            return true;

        }

        public override void SyncChanges()
        {
            this.EnsureChildControls();

            SPPivotViewerWP pivotWP = (SPPivotViewerWP)this.WebPartToEdit;
            if (pivotWP.Type == null)
                ddlType.SelectedIndex = -1;
            else
                ddlType.SelectedValue = pivotWP.Type;
            ddlType_SelectedIndexChanged(null, null);

            if (pivotWP.ListName == null)
                ddlList.SelectedIndex = -1;
            else
                ddlList.SelectedValue = pivotWP.ListName;
            if(ddlType.SelectedValue=="List")
                ddlList_SelectedIndexChanged(this, null);
            if (pivotWP.FieldImage != null)
                ddlImageField.SelectedValue = pivotWP.FieldImage;
            txtImageSubstitue.Text = pivotWP.URLImageSubstitue;
            lstSelectFieldFacet.Items.Clear();
            if (!string.IsNullOrEmpty(pivotWP.FieldDisplay))
            {
                foreach (string str in pivotWP.FieldDisplay.Split('|'))
                    lstSelectFieldFacet.Items.Add(new ListItem(str));
            }

            if (!string.IsNullOrEmpty(pivotWP.Infos))
            {
                foreach (string str in pivotWP.Infos.Split('|'))
                    lstSelectFieldInfo.Items.Add(new ListItem(str));
            }
            ddlBaseList.SelectedValue = pivotWP.BaseList.ToString();
            ddlFieldUrl.SelectedValue = pivotWP.FieldUrl;
            ddlItemOpen.SelectedValue = pivotWP.ItemOpen.ToString();
        }
        private void InstanceControl()
        {
            ddlList = new DropDownList();
            lblBaseList = new Label();
            lblBaseList.Text = "<br/><br/>Choice Base List:<br/>";
            ddlBaseList = new DropDownList();

            lstAllFieldFacet = new ListBox();
            lstAllFieldFacet.SelectionMode = ListSelectionMode.Multiple;
            lstAllFieldFacet.Width = new Unit(100);
            lstSelectFieldFacet = new ListBox();
            lstSelectFieldFacet.SelectionMode = ListSelectionMode.Multiple;
            lstSelectFieldFacet.Width = new Unit(100);
            lstAllFieldInfo = new ListBox();
            lstAllFieldInfo.SelectionMode = ListSelectionMode.Multiple;
            lstAllFieldInfo.Width = new Unit(100);
            lstSelectFieldInfo = new ListBox();
            lstSelectFieldInfo.SelectionMode = ListSelectionMode.Multiple;
            lstSelectFieldInfo.Width = new Unit(100);
            btnAddFieldFacet = new Button();
            btnAddFieldFacet.Click += new EventHandler(btnAddFieldFacet_Click);
            btnRemoveFieldFacet = new Button();
            btnRemoveFieldFacet.Click += new EventHandler(btnRemoveFieldFacet_Click);
            btnAddFieldInfo = new Button();
            btnAddFieldInfo.Click += new EventHandler(btnAddFieldInfo_Click);
            btnRemoveFieldInfo = new Button();
            btnRemoveFieldInfo.Click += new EventHandler(btnRemoveFieldInfo_Click);

            ddlImageField = new DropDownList();
            txtImageSubstitue = new TextBox();
            ddlFieldUrl = new DropDownList();
            ddlItemOpen = new DropDownList();
            ddlItemOpen.Items.Add(new ListItem("New Windows", "1"));
            ddlItemOpen.Items.Add(new ListItem("Same Windows", "2"));

            lcOpenItem = new Label();
            lcOpenItem.Text = "<br/>How do you want to open Item<br/>";
            lcOpenItem.CssClass = "UserSectionHead";

            lcFieldUrl = new Label();
            lcFieldUrl.Text="<br/>Field URL<br/>";
            lcFieldUrl.CssClass = "UserSectionHead";

            lcUrlSubst = new Label();
            lcUrlSubst.Text="<br/>Url of Substitute Image<br/>";
            lcUrlSubst.CssClass = "UserSectionHead";
            
            lcFieldImage = new Label();
            lcFieldImage.Text="Choice Field Image<br/>";
            lcFieldImage.CssClass = "UserSectionHead";
            
            lcFieldInfo = new Label();
            lcFieldInfo.Text="Choice Field as Info";
            lcFieldInfo.CssClass = "UserSectionHead";
            
            lcChoiceFilter = new Label();
            lcChoiceFilter.Text="Choice Field as Filter";
            lcChoiceFilter.CssClass = "UserSectionHead";

            lcChoiceList = new Label();
            lcChoiceList.Text = "<br/>Choice List<br/>";
            lcChoiceList.CssClass = "UserSectionHead"; ;


        }

        private void CreatePanForSiteDataCollection()
        { 

            foreach(SPBaseType b in Enum.GetValues(typeof(SPBaseType)))
            {
                string strValue = ((int)b).ToString();
                string strName=Enum.GetName(typeof(SPBaseType),b);
                ddlBaseList.Items.Add(new ListItem(strName,strValue));
            
            }
            this.Controls.Add(lblBaseList);
            this.Controls.Add(ddlBaseList);
        
        }
        private void CreatePanForListSelection()
        {

            ddlList.SelectedIndexChanged += new EventHandler(ddlList_SelectedIndexChanged);
            ddlList.AutoPostBack = true;
            ddlList.Items.Add(new ListItem("Select List", ""));

            SPWeb web = SPContext.Current.Web;
            foreach (SPList lst in web.Lists)
            {
                ddlList.Items.Add(new ListItem(lst.Title));
            }

            this.Controls.Add(lcChoiceList);
            this.Controls.Add(ddlList);


            tblFacet = new Table();
            this.Controls.Add(tblFacet);
            tblFacet.ID = "FieldFacetChoiceTbl";
            TableRow trFacet = new TableRow();
            trFacet.ID = "FieldFacetChoiceHeader";
            tblFacet.Controls.Add(trFacet);
            TableCell tcFacet = new TableCell();
            tcFacet.ColumnSpan = 3;
            tcFacet.Controls.Add(lcChoiceFilter);
            trFacet.Cells.Add(tcFacet);


            trFacet = new TableRow();
            trFacet.ID = "FieldFacetChoiceTr";
            tblFacet.Controls.Add(trFacet);

            tcFacet = new TableCell();
            tcFacet.ID = "FieldFacetChoiceTcAll";
            tcFacet.Controls.Add(lstAllFieldFacet);
            trFacet.Controls.Add(tcFacet);

            tcFacet = new TableCell();
            tcFacet.ID = "FieldFacetChoiceTcbtn";
            tcFacet.Controls.Add(btnAddFieldFacet);
            tcFacet.Controls.Add(new LiteralControl("<br/>"));
            tcFacet.Controls.Add(btnRemoveFieldFacet);
            trFacet.Controls.Add(tcFacet);


            tcFacet = new TableCell();
            tcFacet.ID = "FieldFacetChoiceTcSelected";
            tcFacet.Controls.Add(lstSelectFieldFacet);
            trFacet.Controls.Add(tcFacet);

            btnAddFieldFacet.ID = "btnAddFieldFacet";
            btnAddFieldFacet.Text = ">";
            btnRemoveFieldFacet.ID = "btnRemoveFieldFacet";
            btnRemoveFieldFacet.Text = "<";


            tblInfo = new Table();
            this.Controls.Add(tblInfo);
            tblInfo.ID = "FieldInfoChoiceTbl";
            TableRow trInfo = new TableRow();
            trInfo.ID = "FieldInfoChoiceHeader";
            tblInfo.Controls.Add(trInfo);
            TableCell tcInfo = new TableCell();
            tcInfo.ColumnSpan = 3;
            tcInfo.Controls.Add(lcFieldInfo);
            trInfo.Cells.Add(tcInfo);

            trInfo = new TableRow();
            trInfo.ID = "FieldInfoChoiceTr";
            tblInfo.Controls.Add(trInfo);

            tcInfo = new TableCell();
            tcInfo.ID = "FieldInfoChoiceTcAll";
            tcInfo.Controls.Add(lstAllFieldInfo);
            trInfo.Controls.Add(tcInfo);

            tcInfo = new TableCell();
            tcInfo.ID = "FieldInfoChoiceTcbtn";
            tcInfo.Controls.Add(btnAddFieldInfo);
            tcInfo.Controls.Add(new LiteralControl("<br/>"));
            tcInfo.Controls.Add(btnRemoveFieldInfo);
            trInfo.Controls.Add(tcInfo);


            tcInfo = new TableCell();
            tcInfo.ID = "FieldInfoChoiceTcSelected";
            tcInfo.Controls.Add(lstSelectFieldInfo);
            trInfo.Controls.Add(tcInfo);

            btnAddFieldInfo.ID = "btnAddFieldInfo";
            btnAddFieldInfo.Text = ">";
            btnRemoveFieldInfo.ID = "btnRemoveFieldInfo";
            btnRemoveFieldInfo.Text = "<";



            this.Controls.Add(lcFieldImage);
            this.Controls.Add(ddlImageField);
            ddlImageField.Width = new Unit(250);


            this.Controls.Add(lcUrlSubst);
            this.Controls.Add(txtImageSubstitue);
            txtImageSubstitue.Width = new Unit(250);


            this.Controls.Add(lcFieldUrl);
            this.Controls.Add(ddlFieldUrl);


            this.Controls.Add(lcOpenItem);
            this.Controls.Add(ddlItemOpen);

            EnableControlListSelected(false);

        }


    }
}
